package utils;

import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Created by 郭浩 on 2020/9/23 15:29
 */
public class DBCPUtils {
    private static DataSource dataSource;

    static {
        InputStream resourceAsStream = DBCPUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
        Properties properties = new Properties();

        try {
            properties.load(resourceAsStream);
            BasicDataSourceFactory basicDataSourceFactory = new BasicDataSourceFactory();
            dataSource = basicDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
}
